package com.example.smartcity_40.lawyer;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.smartcity_40.Outpatient.bean.StatusBean;
import com.example.smartcity_40.R;
import com.example.smartcity_40.lawyer.bean.LawyerBannerBean;
import com.example.smartcity_40.lawyer.bean.LawyerBean;
import com.example.smartcity_40.tool.Tool;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import kotlin.Unit;
import kotlin.jvm.functions.Function1;

public class SeekLawyerActivity extends AppCompatActivity {

    private SeekLawyerActivity context;
    private ImageView avatarUrl;
    private TextView name;
    private TextView legalExpertiseName;
    private TextView serviceTimes;
    private TextView favorableCount;
    private String lawyerid;
    private String legalExpertiseid;
    private Tool tool;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seek_lawyer);
        context = this;
        initview();
        getinformationlawyer();
    }

    private void getinformationlawyer() {
        tool.send("/prod-api/api/lawyer-consultation/lawyer/"+lawyerid,
                Tool.GET,
                null,
                true,
                LawyerBean.class,
                new Function1<LawyerBean, Unit>() {
                    @Override
                    public Unit invoke(LawyerBean lawyerBean) {
                        if (lawyerBean.getCode() == 200) {
                            initlawyer(lawyerBean.getData());
                        }
                        return null;
                    }
                });
    }


    private void initlawyer(LawyerBean.DataBean data) {
        try {
            if(data ==null){
                return;
            }
            String dataAvatarUrl = data.getAvatarUrl();
            if(!TextUtils.isEmpty(dataAvatarUrl)){
                Glide.with(context).load(Tool.url+dataAvatarUrl).into(avatarUrl);
            }else {
                Glide.with(context).load(Tool.url+dataAvatarUrl)
                        .error(R.drawable.waimai)
                        .into(avatarUrl);
            }

            String dataName = data.getName();
            if(!TextUtils.isEmpty(dataName)){
                name.setText(dataName);
            }

            String dataLegalExpertiseName = data.getLegalExpertiseName();
            if(!TextUtils.isEmpty(dataLegalExpertiseName)){
                legalExpertiseName.setText("专长: "+dataLegalExpertiseName);
            }

            Integer dataServiceTimes = data.getServiceTimes();
            if(dataServiceTimes !=null ){
                serviceTimes.setText("咨询人数： "+String.valueOf(dataServiceTimes));
            }


            Integer dataFavorableCount = data.getFavorableCount();
            if(dataFavorableCount !=null ){
                favorableCount.setText("服务次数： "+String.valueOf(dataFavorableCount));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void initview() {
        ImageView back = findViewById(R.id.back);
        EditText ed_phone = findViewById(R.id.ed_phone);
        EditText ed_content = findViewById(R.id.ed_content);
        TextView submit = findViewById(R.id.submit);
        Intent intent = getIntent();
        lawyerid = intent.getStringExtra("lawyerid");
        legalExpertiseid = intent.getStringExtra("LegalExpertiseId");
        tool = new Tool(context);

        avatarUrl = findViewById(R.id.avatarUrl);
        name = findViewById(R.id.name);
        legalExpertiseName = findViewById(R.id.legalExpertiseName);
        serviceTimes = findViewById(R.id.serviceTimes);
        favorableCount = findViewById(R.id.favorableCount);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.finish();
            }
        });

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    String phone = ed_phone.getText().toString();
                    String content = ed_content.getText().toString();
                    if(!TextUtils.isEmpty(phone.trim()) && !TextUtils.isEmpty(content.trim())
                        && !TextUtils.isEmpty(lawyerid.trim())
                        && !TextUtils.isEmpty(legalExpertiseid.trim())
                    ){
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("lawyerId",lawyerid);
                        jsonObject.put("legalExpertiseId",legalExpertiseid);
                        jsonObject.put("content",content);
                        jsonObject.put("phone",phone);
                        addlawyerseek(jsonObject.toString());
                    }else {
                        Toast.makeText(context,"填写的信息不能为空",Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void addlawyerseek(String JSON) {
        tool.send("/prod-api/api/lawyer-consultation/legal-advice",
                Tool.POST,
                JSON,
                true,
                StatusBean.class,
                new Function1<StatusBean, Unit>() {
                    @Override
                    public Unit invoke(StatusBean statusBean) {
                        if (statusBean.getCode() == 200) {
                            Toast.makeText(context,"提问成功",Toast.LENGTH_SHORT).show();
                            context.finish();
                        }else {
                            Toast.makeText(context,statusBean.getMsg(),Toast.LENGTH_SHORT).show();
                        }
                        return null;
                    }
                });
    }


}

/*
* 上部显示律师信息，包括头像、名称、从业年限、
咨询人数、法律专长和好评率；下部显示问题信息。内容包括：受理
状态、问题类型、问题描述、上传的图片和联系电话；如果咨询已完
结，则底部显示评价按钮，点击评价按钮页面跳转至服务评价
* */